<?php
/**
 * Introduction: 微店红包明细表  micro_shop_bonus_detail
 */

namespace Common\Model;


use Think\Model;

class MicroShopBonusDetailModel extends Model
{
    //定义数据库连接信息
    protected $connection;

    //定义表名
    protected $tableName = 'micro_shop_bonus_detail';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }

    /**
     * 获取等待收益的订单列表，注意返回数据的格式
     * @param integer $memberId
     * @param integer $currentPage
     * @param integer $pageSize
     * @param integer $isNeedTotal
     * @return array : watitingEarnOrderIds: key - 红包ID，order_id - 订单ID
     */
    public function getEarnOrderIds($memberId, $num, $limit)
    {
        $fields = [
            'a.id',
            'b.order_id',
            'c.order_id as order_id2'
        ];
        $records = $this->alias('a')->join("left join haiou_micro_shop_bonus_sales as b on a.id=b.bonus_id left join haiou_micro_shop_bonus_service as c on a.id=c.bonus_id")->where("a.member_id=$memberId and ((a.status=0 and a.p_settle_time > '".date('Y-m-d H:i:s')."') or a.status=-1)")->field($fields)->limit($num,$limit)->select();
        $bonusOrderId = [];
        if (!empty($records)) {
            //存在未领取的红包
            foreach ($records as $v) {
                $orderId = $v['order_id'];
                if ($orderId == null && $v['order_id2'] != '') {
                    $orderId = $v['order_id2'];
                }
                $bonusOrderId[$v['id']] = $orderId;
            }
        }
        $result = ['watitingEarnOrderIds' => $bonusOrderId];
        return $result;
    }

    /**
     * 获取累计收益的订单列表，注意返回数据的格式
     * @param integer $memberId
     * @param integer $currentPage
     * @param integer $pageSize
     * @param integer $isNeedTotal
     * @return array : watitingEarnOrderIds: key - 红包ID，order_id - 订单ID
     */
    public function getEarnOrderIds1($memberId, $num, $limit)
    {
        $fields = [
            'a.id',
            'b.order_id',
            'c.order_id as order_id2'
        ];
        $records = $this->alias('a')->join("left join haiou_micro_shop_bonus_sales as b on a.id=b.bonus_id left join haiou_micro_shop_bonus_service as c on a.id=c.bonus_id")->where("a.member_id=$memberId ")->field($fields)->limit($num,$limit)->select();
        $bonusOrderId = [];
        if (!empty($records)) {
            //存在未领取的红包
            foreach ($records as $v) {
                $orderId = $v['order_id'];
                if ($orderId == null && $v['order_id2'] != '') {
                    $orderId = $v['order_id2'];
                }
                if(!empty($orderId)){
                    $bonusOrderId[$v['id']] = $orderId;
                }
            }
        }
        $result = ['watitingEarnOrderIds' => $bonusOrderId];
        return $result;
    }

    /**
     * 获取订单订单红包详情
     * @param integer $memberId
     * @param string $orderId
     */
    public function getInfoById($id)
    {
        $columns = ['id','price','p_settle_time','status','get_time'];
        $record = $this->where("id=$id")->field($columns)->find();
        return $record;
    }

}